package com.bac.rentmap.web.controller.ad.manage;

import java.util.List;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Required;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.bac.rentmap.ad.flat.FlatAd;
import com.bac.rentmap.ad.flat.FlatAdDao;
import com.bac.rentmap.ad.flat.rating.BetterFlatAdProvider;
import com.bac.rentmap.ad.flat.rating.FlatAdRatingCalculator;
import com.bac.rentmap.ad.flat.rating.update.NormalizedRatingIncrementEstimatingService;
import com.bac.rentmap.ad.flat.rating.update.NormalizedRatingIncrementEstimation;
import com.bac.rentmap.ad.flat.rating.update.RatingAwareFlatAdUpdater;

public class RatingEditorController extends MultiActionController {
    private static final Logger log = Logger
            .getLogger(RatingEditorController.class.getName());
    private FlatAdDao flatAdDao;
    private NormalizedRatingIncrementEstimatingService normalizedRatingIncrementEstimatingService;
    private RatingAwareFlatAdUpdater lastPublishingDateUpdater;
    private FlatAdRatingCalculator flatAdRatingCalculator;
    private BetterFlatAdProvider betterFlatAdProvider;

    public ModelAndView view(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        String secureId = request.getParameter("secureId");
        log.fine("About to view flatAd, secureId: " + secureId);
        FlatAd flatAd = flatAdDao.loadBySecureId(secureId);
        ModelAndView res = new ModelAndView("ad/manage/ratingEditor/view");
        res.addObject("flatAd", flatAd);
        List<NormalizedRatingIncrementEstimation> ratingIncrementEstimations = normalizedRatingIncrementEstimatingService
                .estimate(secureId);
        res.addObject("ratingIncrementEstimations", ratingIncrementEstimations);
        long rating = flatAd.getRating();
        res.addObject("betterFlatAdCount", betterFlatAdProvider
                .countFlatAdsWithBetterRating(flatAd.getCity(), rating));
        res.addObject("normalizedRating", flatAdRatingCalculator
                .normalizeRating(rating));
        return res;
    }

    public ModelAndView updateLastPublishingDate(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        String secureId = request.getParameter("secureId");
        log.fine("About to update LastPublishingDate, secureId: " + secureId);
        lastPublishingDateUpdater.update(secureId);
        return view(request, response);
    }

    @Required
    public void setFlatAdDao(FlatAdDao flatAdDao) {
        this.flatAdDao = flatAdDao;
    }

    @Required
    public void setNormalizedRatingIncrementEstimatingService(
            NormalizedRatingIncrementEstimatingService normalizedRatingIncrementEstimatingService) {
        this.normalizedRatingIncrementEstimatingService = normalizedRatingIncrementEstimatingService;
    }

    @Required
    public void setLastPublishingDateUpdater(
            RatingAwareFlatAdUpdater lastPublishingDateUpdater) {
        this.lastPublishingDateUpdater = lastPublishingDateUpdater;
    }

    @Required
    public void setFlatAdRatingCalculator(
            FlatAdRatingCalculator flatAdRatingCalculator) {
        this.flatAdRatingCalculator = flatAdRatingCalculator;
    }

    @Required
    public void setBetterFlatAdProvider(
            BetterFlatAdProvider betterFlatAdProvider) {
        this.betterFlatAdProvider = betterFlatAdProvider;
    }
}
